#include "motor.h"


void MotorAll_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14 | GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	PWM12_Init();
}

void MotorR_SetSpeed(int8_t Speed)
{
	if (Speed >= 0)//Speed值为正
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_12);//电机正转
		GPIO_ResetBits(GPIOB, GPIO_Pin_13);
		PWM12_SetCompare1(Speed);//设置Speed转速
	}
	else//Speed值为负
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);//电机反转
		GPIO_SetBits(GPIOB, GPIO_Pin_13);
		PWM12_SetCompare1(-Speed);//设为-Speed转速
	}
}

void MotorL_SetSpeed(int8_t Speed)
{
	if (Speed >= 0)
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_14);
		GPIO_ResetBits(GPIOB, GPIO_Pin_15);
		PWM12_SetCompare2(Speed);
	}
	else
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_14);
		GPIO_SetBits(GPIOB, GPIO_Pin_15);
		PWM12_SetCompare2(-Speed);
	}
}



void Car_Stop(void)//小车停止
{
	MotorR_SetSpeed(0);
	MotorL_SetSpeed(0);
}

void Car_Up(void)//小车前进
{
	MotorR_SetSpeed(100);
	MotorL_SetSpeed(100);	
}

void Car_Down(void)//小车后退
{
	MotorR_SetSpeed(-100);
	MotorL_SetSpeed(-100);
}

void Car_TurnRight(void)//小车右转
{
	MotorR_SetSpeed(50);
	MotorL_SetSpeed(100);
}

void Car_TurnLeft(void)//小车左转
{
	MotorR_SetSpeed(100);
	MotorL_SetSpeed(50);
}

void Car_Spin_L(void)//小车左旋转
{
	MotorR_SetSpeed(100);
	MotorL_SetSpeed(-100);
}

void Car_Spin_R(void)//小车右旋转
{
	MotorR_SetSpeed(-100);
	MotorL_SetSpeed(100);
}



